package com.taptech.doufu.ui.view.topicview;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.CategoryGsonBean;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicCategorytView extends LinearLayout implements View.OnClickListener{
    private TextView tvTitle;
    private String title;
    private String type;
    private String pageType;

    private List<DFHomeNovelBeans> list;
    public NovelTopicCategorytView(Context context) {
        super(context);
    }

    public NovelTopicCategorytView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicCategorytView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private View root_layout;
    private View linearLayout0;
    private View topSpacView;
    private Context context;
    private View rl_dfnovel_layout1;

    private String urlOne,urlTwo,urlThree;
    private String nameOne,nameTwo,nameThree;
    private EventBean eventBean;

    public NovelTopicCategorytView(final Context context, DFHomeNovelContentBeans model, final String type,String pageType){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_category_view, null);
        this.context = context;
        this.type = type;
        this.pageType = pageType;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        rl_dfnovel_layout1 = view.findViewById(R.id.rl_dfnovel_layout1);
        topSpacView = view.findViewById(R.id.home_dfnovel_layout_space);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        tvTitle.setText(model.getTitle());
        title = model.getTitle();
        eventBean = model.getEventData();
        initContentView(view);
        changeDayModelBg();
        list = model.getList();
        setCategoryTitle(list);
        setData(list);
        addView(view);
    }

    private void setCategoryTitle(List<DFHomeNovelBeans> list) {
        try {
            urlOne = list.get(0).getAlbumInfo().getUrl();
            urlTwo = list.get(1).getAlbumInfo().getUrl();
            urlThree = list.get(2).getAlbumInfo().getUrl();
            nameOne = list.get(0).getAlbumInfo().getName();
            nameTwo = list.get(1).getAlbumInfo().getName();
            nameThree= list.get(2).getAlbumInfo().getName();
            tvBtnOne.setText(nameOne);
            tvBtnTwo.setText(nameTwo);
            tvBtnThree.setText(nameThree);
            getData(urlOne,1);
        } catch (Exception e) {

        }
    }

    private TextView title_1,title_2,title_3;
    private ImageView ivPic_1,ivPic_2,ivPic_3;
    private ImageView ivVip_1,ivVip_2,ivVip_3;
    private ImageView ivNum_1,ivNum_2,ivNum_3;
    private View lay_1,lay_2,lay_3;
    private LinearLayout llBtnOne,llBtnTwo,llBtnThree;
    private TextView tvBtnThree,tvBtnTwo,tvBtnOne;
    public void initContentView(View view){
        llBtnOne = (LinearLayout) view.findViewById(R.id.llBtnOne);
        llBtnOne.setOnClickListener(this);
        llBtnTwo = (LinearLayout) view.findViewById(R.id.llBtnTwo);
        llBtnTwo.setOnClickListener(this);
        llBtnThree = (LinearLayout) view.findViewById(R.id.llBtnThree);
        llBtnThree.setOnClickListener(this);
        tvBtnThree = (TextView) view.findViewById(R.id.tvBtnThree);
        tvBtnTwo = (TextView) view.findViewById(R.id.tvBtnTwo);
        tvBtnOne = (TextView) view.findViewById(R.id.tvBtnOne);
        title_1 = (TextView) view.findViewById(R.id.title_1);
        ivPic_1 = (ImageView) view.findViewById(R.id.ivPic_1);
        ivVip_1 = (ImageView) view.findViewById(R.id.ivVip_1);
        ivNum_1 = (ImageView) view.findViewById(R.id.ivNum_1);
        title_2 = (TextView) view.findViewById(R.id.title_2);
        ivPic_2 = (ImageView) view.findViewById(R.id.ivPic_2);
        ivVip_2 = (ImageView) view.findViewById(R.id.ivVip_2);
        ivNum_2 = (ImageView) view.findViewById(R.id.ivNum_2);
        title_3 = (TextView) view.findViewById(R.id.title_3);
        ivPic_3 = (ImageView) view.findViewById(R.id.ivPic_3);
        ivVip_3 = (ImageView) view.findViewById(R.id.ivVip_3);
        ivNum_3 = (ImageView) view.findViewById(R.id.ivNum_3);
        lay_1 = view.findViewById(R.id.lay_1);
        lay_2 = view.findViewById(R.id.lay_2);
        lay_3 = view.findViewById(R.id.lay_3);
        selectOne();
    }

    private void getData(String url, final int type) {
        ApiClient.getInstance().setUseCache(false).getService()
                .getCategoryData(url)
                .compose(RxJavaHelper.<CategoryGsonBean>observeOnMainThread((Activity) context))
                .subscribe(new BaseSubscriber<CategoryGsonBean>(){
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onNext(CategoryGsonBean bean) {
                        super.onNext(bean);
                        if (bean.getData() != null && bean.getData().getItems() != null) {
                            if (type == 1) {
                                listOne = bean.getData().getItems();
                            } else if (type == 2) {
                                listTwo = bean.getData().getItems();
                            } else if (type == 3) {
                                listThree = bean.getData().getItems();
                            }
                            setData(bean.getData().getItems());
                        }
                    }
                });
    }

    private void setData(List<DFHomeNovelBeans> list){
        if (list != null) {
            switch (list.size()) {
                case 0:
                    lay_1.setVisibility(INVISIBLE);
                    lay_2.setVisibility(INVISIBLE);
                    lay_3.setVisibility(INVISIBLE);
                    break;
                case 1:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(INVISIBLE);
                    lay_3.setVisibility(INVISIBLE);
                    break;
                case 2:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(INVISIBLE);
                    break;
                default:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(VISIBLE);
                    break;
            }
            for (int i = 0; i < list.size(); i++) {
                final DFHomeNovelBeans model = list.get(i);
                if (model != null) {
                    if (i == 0) {
                        title_1.setText(model.getTitle());
                        if (TextUtils.isEmpty(model.getCover_left_top_icon_url())) {
                            if ("1".equals(model.getIs_vip())) {
                                ivVip_1.setVisibility(View.VISIBLE);
                            } else {
                                ivVip_1.setVisibility(View.GONE);
                            }
                            ivNum_1.setVisibility(View.GONE);
                        } else {
                            ivNum_1.setVisibility(View.VISIBLE);
                            ivVip_1.setVisibility(View.GONE);
                            GlideUtil.displayImage(context,ivNum_1,model.getCover_left_top_icon_url());
                        }
                        if (model.getImages() != null && model.getImages().length > 0) {
                            if ((model.getImages()[0].getImgUrl() != null) && (model.getImages()[0].getImgUrl().length() > 0)) {
                                GlideUtil.displayRoundImage(context,ivPic_1,model.getImages()[0].getImgUrl(),3,R.drawable.img_default_loading);
                            }
                        }
                        lay_1.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UmengEventUtil.addContentEvent(context,eventBean,type,pageType);
                                DfhomeNovelAdapter3.goToNovelDesActivity(context, String.valueOf(model.getId()),
                                        model.getObject_type(), model.getTitle(), type,title);
                            }
                        });
                    } else if (i == 1) {
                        title_2.setText(model.getTitle());
                        if (TextUtils.isEmpty(model.getCover_left_top_icon_url())) {
                            if ("1".equals(model.getIs_vip())) {
                                ivVip_2.setVisibility(View.VISIBLE);
                            } else {
                                ivVip_2.setVisibility(View.GONE);
                            }
                            ivNum_2.setVisibility(View.GONE);
                        } else {
                            ivNum_2.setVisibility(View.VISIBLE);
                            ivVip_2.setVisibility(View.GONE);
                            GlideUtil.displayImage(context,ivNum_2,model.getCover_left_top_icon_url());

                        }
                        if (model.getImages() != null && model.getImages().length > 0) {
                            if ((model.getImages()[0].getImgUrl() != null) && (model.getImages()[0].getImgUrl().length() > 0)) {
                                GlideUtil.displayRoundImage(context,ivPic_2,model.getImages()[0].getImgUrl(),3,R.drawable.img_default_loading);
                            }
                        }
                        lay_2.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UmengEventUtil.addContentEvent(context,eventBean,type,pageType);
                                DfhomeNovelAdapter3.goToNovelDesActivity(context, String.valueOf(model.getId()),
                                        model.getObject_type(), model.getTitle(), type,title);
                            }
                        });
                    } else if (i == 2) {
                        title_3.setText(model.getTitle());
                        if (TextUtils.isEmpty(model.getCover_left_top_icon_url())) {
                            if ("1".equals(model.getIs_vip())) {
                                ivVip_3.setVisibility(View.VISIBLE);
                            } else {
                                ivVip_3.setVisibility(View.GONE);
                            }
                            ivNum_3.setVisibility(View.GONE);
                        } else {
                            ivNum_3.setVisibility(View.VISIBLE);
                            ivVip_3.setVisibility(View.GONE);
                            GlideUtil.displayImage(context,ivNum_3,model.getCover_left_top_icon_url());
                        }
                        if (model.getImages() != null && model.getImages().length > 0) {
                            if ((model.getImages()[0].getImgUrl() != null) && (model.getImages()[0].getImgUrl().length() > 0)) {
                                GlideUtil.displayRoundImage(context,ivPic_3,model.getImages()[0].getImgUrl(),3,R.drawable.img_default_loading);
                            }
                        }
                        lay_3.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UmengEventUtil.addContentEvent(context,eventBean,type,pageType);
                                DfhomeNovelAdapter3.goToNovelDesActivity(context, String.valueOf(model.getId()),
                                        model.getObject_type(), model.getTitle(), type,title);
                            }
                        });
                    } else {
                        break;
                    }
                }
            }
        }
    }

    public void refrush(DFHomeNovelContentBeans model) {
        if (model == null || !TextUtils.isEmpty(model.getTitle()) && model.getTitle().equals(title)) {
            return;
        }
        title = model.getTitle();
        list = model.getList();
        if (tvTitle != null) {
            tvTitle.setText(model.getTitle());
        }
        setData(list);

    }

    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_8));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.all_activity_bg));
        rl_dfnovel_layout1.setBackgroundColor(context.getResources().getColor(R.color.white));
        //第一个布局
        lay_1.setBackgroundColor(context.getResources().getColor(R.color.white));
        title_1.setTextColor(context.getResources().getColor(R.color.text_color_22));
        ivPic_1.setAlpha(1.0f);
        ivVip_1.setAlpha(1.0f);
        ivNum_1.setAlpha(1.0f);
        //第二个布局
        lay_2.setBackgroundColor(context.getResources().getColor(R.color.white));
        title_2.setTextColor(context.getResources().getColor(R.color.text_color_22));
        ivPic_2.setAlpha(1.0f);
        ivVip_2.setAlpha(1.0f);
        ivNum_2.setAlpha(1.0f);
        //第三个布局
        lay_3.setBackgroundColor(context.getResources().getColor(R.color.white));
        title_3.setTextColor(context.getResources().getColor(R.color.text_color_22));
        ivPic_3.setAlpha(1.0f);
        ivVip_3.setAlpha(1.0f);
        ivNum_3.setAlpha(1.0f);
    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        rl_dfnovel_layout1.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        //第一个布局
        lay_1.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        title_1.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        ivPic_1.setAlpha(1.0f);
        ivVip_1.setAlpha(1.0f);
        ivNum_1.setAlpha(1.0f);
        //第二个布局
        lay_2.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        title_2.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        ivPic_2.setAlpha(1.0f);
        ivVip_2.setAlpha(1.0f);
        ivNum_2.setAlpha(1.0f);
        //第三个布局
        lay_3.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        title_3.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        ivPic_3.setAlpha(Constant.imagViewAlpha);
        ivVip_3.setAlpha(Constant.imagViewAlpha);
        ivNum_3.setAlpha(Constant.imagViewAlpha);
    }

    private List<DFHomeNovelBeans> listOne,listTwo,listThree;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llBtnOne:
                selectOne();
                if (listOne != null && listOne.size() > 0) {
                    setData(listOne);
                }else{
                    getData(urlOne,1);
                }
                break;
            case R.id.llBtnTwo:
                selectTwo();
                if (listTwo != null && listTwo.size() > 0) {
                    setData(listTwo);
                }else{
                    getData(urlTwo,2);
                }
                break;
            case R.id.llBtnThree:
                selectThree();
                if (listThree != null && listThree.size() > 0) {
                    setData(listThree);
                }else{
                    getData(urlThree,3);
                }
                break;
        }
    }

    private void selectThree() {
        llBtnOne.setSelected(false);
        llBtnTwo.setSelected(false);
        llBtnThree.setSelected(true);
        tvBtnOne.setTextColor(getResources().getColor(R.color.top_title_red_color));
        tvBtnTwo.setTextColor(getResources().getColor(R.color.top_title_red_color));
        tvBtnThree.setTextColor(getResources().getColor(R.color.white));
    }

    private void selectTwo() {
        llBtnOne.setSelected(false);
        llBtnTwo.setSelected(true);
        llBtnThree.setSelected(false);
        tvBtnOne.setTextColor(getResources().getColor(R.color.top_title_red_color));
        tvBtnTwo.setTextColor(getResources().getColor(R.color.white));
        tvBtnThree.setTextColor(getResources().getColor(R.color.top_title_red_color));
    }

    private void selectOne() {
        llBtnOne.setSelected(true);
        llBtnTwo.setSelected(false);
        llBtnThree.setSelected(false);
        tvBtnOne.setTextColor(getResources().getColor(R.color.white));
        tvBtnTwo.setTextColor(getResources().getColor(R.color.top_title_red_color));
        tvBtnThree.setTextColor(getResources().getColor(R.color.top_title_red_color));
    }
}
